// pages/payDetail/index.js
const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgWidth: 0, imgHeight: 0,
    pice: 0,
    note: [
      {
        original: 10,
        discounte:0
      },
      {
        original: 50,
        discounte: 0
      },
      {
        original: 100,
        discounte: 0
      },
      {
        original: 200,
        discounte: 0
      },
      {
        original: 500,
        discounte: 0
      },
      {
        original: 1000,
        discounte: 0
      },
    ]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    wx.request({
      url: app.globalData.basicUrl + 'user/userPolicy/ids',
      // url:"http://stcar.easy.echosite.cn/userPolicy/1",
      success: function (res) {
        let note=that.data.note
        // let note = res.data.data.note
        console.log(res);
        for (var i = 0; i < note.length; i++) {
          note[i].discounte = note[i].original * res.data.data.discounte / 100
        }
        that.setData({
          note: note
        })
      }
    })

  },

  //充值
  recharge: function (e) {
    var pice = e.currentTarget.dataset.id
    console.log(pice)
    this.setData({
      pice: pice
    })
  },




  pay: function () {
    var that = this;
    var userId = wx.getStorageSync('userId')
    console.log("openId", app.globalData.openId);
    console.log("user", wx.getStorageSync('userId'));
    wx.request({
       url: app.globalData.basicUrl + 'user/WxRecharge/recharge',
      // url:"http://stcar.easy.echosite.cn/WxRecharge/recharge",
      method: "POST",
      data: {
        openId: app.globalData.openId,
        userId: wx.getStorageSync('userId'),
        money: that.data.pice
      },
      success: function (res) {
        var orderId = this;
        console.log(res);
        wx.requestPayment({
          timeStamp: res.data.timeStamp,
          package: res.data.package,
          paySign: res.data.paySign,
          appId: res.data.appId,
          signType: res.data.signType,
          nonceStr: res.data.nonceStr,
          success: function (res) {
            wx.showToast({
              title: '充值成功',
              duration: 2000,
              success: function () {
                setTimeout(() => {
                  wx.navigateBack({
                    delta: 1
                  })
                }, 2000)
              }
            })
          }
        })
      },
      fail: function (error) {
        console.log(error);
        wx.showToast({
          title: '充值失败',
          duration: 2000,
          success: function () {
            setTimeout(() => {
              wx.navigateTo({
                url: '/pages/ucenter/payList/index',
              })
            }, 2000)
          }
        })
      }
    })
  },
  fail: function (error) {
    console.log(error);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    app.getSetting(function(){})
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})

